const User = require("../../model/User")

module.exports = async (req, res) => {
    // 检查权限
    if (!res.isrole()) {
        return
    }
    const { id, capacity } = req.body
    if (!id) {
        res.sendResult(null, 400, '请传递用户 ID')
        return
    }
    if (!capacity) {
        res.sendResult(null, 400, '请传递用户容量值')
        return
    }
    const findUser = await User.findOne({ where: { id } })
    if (!findUser) {
        res.sendResult(null, 400, '用户不存在！')
        return
    }

    // 当前容量值
    let editcapacity = findUser.capacity + Number(capacity)
    // 最大容量值
    let maxcapacity = findUser.maxcapacity + Number(capacity)
    const editcpt = await User.update({ capacity: editcapacity, maxcapacity }, { where: { id } })
    if (!editcpt) {
        res.sendResult(null, 500, '修改失败！发生未知错误')
        return
    }
    res.sendResult({ capacity: editcapacity, maxcapacity }, 200, '修改用户容量值成功！')
}